package scau.erps.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;

import scau.erps.app.Erps;
import scau.erps.entity.Factory;
import scau.erps.entity.Team;
import scau.erps.service.FactoryService;
import scau.erps.service.impl.FactoryServiceImpl;
import scau.erps.view.base.CopyrightPanel;
import scau.erps.view.base.DecisionPanel;
import scau.erps.view.base.SandDialog;
import scau.erps.view.business.team.MyBusinessDialog;

/**
 * 主程序界面
 * @author DCJ
 */
public class MainFrame extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JSplitPane splitPanel;
	private SandPanel dashBoardPanel; //切换到Swing模拟沙盘界面
	//private DashBoardPanel dashBoardPanel;
	private JButton btnsand,btntx,btngld,btnmyReport,btnmyBiz,btnzycc,btnbmcc,btngmcc;
	private Team team = Erps.getCurrentTeam();	
	private FactoryService factoryService=Erps.getIocInstance().get(FactoryServiceImpl.class);
	public MainFrame(){		
		this.setTitle("ERP沙盘教学辅助系统");
		this.setUndecorated(true);
		//this.setBounds(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); //全屏
		this.setSize(1280,960);
		this.setResizable(false);
		//this.setExtendedState(JFrame.MAXIMIZED_BOTH); 
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel contentPanel = new JPanel(new BorderLayout());		
		contentPanel.add(getToolBar(),BorderLayout.NORTH);
		contentPanel.add(getSplitPanel(),BorderLayout.CENTER);
		contentPanel.add(new CopyrightPanel(), BorderLayout.SOUTH);
		this.add(contentPanel);	
		this.setVisible(true);
		}
	/**
	 * 工具栏
	 */
	private JToolBar getToolBar(){
		JToolBar toolBar = new JToolBar();		
		btnsand = new JButton("沙盘模拟");		
		btntx = new JButton("申请贴现");
		btngld = new JButton("申请高利贷");		
		btnzycc =new JButton("租用厂房");
		btngmcc =new JButton("购买厂房");
		btnbmcc =new JButton("变卖厂房");
		btnmyReport = new JButton("我的报表");
		btnmyBiz = new JButton("我的业务");
		btnsand.addActionListener(this);		
		btnzycc.addActionListener(this);
		btngmcc.addActionListener(this);
		btnbmcc.addActionListener(this);
		btnmyBiz.addActionListener(this);
		btnmyReport.addActionListener(this);
		toolBar.add(btnsand);		
		toolBar.addSeparator();
		toolBar.add(btntx);
		toolBar.add(btngld);		
		toolBar.addSeparator(); 
		toolBar.add(btnzycc);
		toolBar.add(btngmcc);
		toolBar.add(btnbmcc);
		toolBar.addSeparator();		
		toolBar.add(btnmyBiz);
		toolBar.add(btnmyReport);
		return toolBar;			
		}	
	
	/**
	 * 分隔面板
	 */
	private JSplitPane getSplitPanel(){
		if(splitPanel==null){
			splitPanel = new JSplitPane();
		}
		dashBoardPanel = new SandPanel();  //一句切换到Swing模拟沙盘
		//dashBoardPanel = new DashBoardPanel();
		DecisionPanel decisionPanel = new DecisionPanel(dashBoardPanel);
		JScrollPane leftPane = new JScrollPane(decisionPanel) ;
		splitPanel.add(leftPane, JSplitPane.LEFT);				
		JScrollPane rightPane = new JScrollPane(dashBoardPanel) ;
		splitPanel.add(rightPane, JSplitPane.RIGHT);	
		splitPanel.setDividerLocation(360);
		splitPanel.setContinuousLayout(true);
	    splitPanel.setEnabled(false);
		return splitPanel;	
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {		
		//沙盘模拟	
		if (e.getSource() == btnsand) {
			new SandDialog();
		}
		
		//租用厂房
		else if (e.getSource() == btnzycc) {
            List<Factory> list=factoryService.getTeamFactoyByStatus(team.getId(), "未购买");       //获取当前小组可租用厂房
            if (list.isEmpty()==false)
            {
            	List<String> type=new ArrayList<>();
            	for (Factory factory :list)
            		type.add(factory.getType());
   				String s = (String) JOptionPane.showInputDialog(null,
						"请选择租用厂房类型:\n", "租用厂房", JOptionPane.PLAIN_MESSAGE, null,
						type.toArray(), type.get(0));
   			    if(s!=null){
   			    	factoryService.rentFactoy(team.getId(),s);
   					JOptionPane.showMessageDialog(this, "租用"+s+"成功", "提示",
							JOptionPane.INFORMATION_MESSAGE);   			    	
   			   
            }
            }
            else  JOptionPane.showMessageDialog(this, "当前无可租用厂房", "提示",JOptionPane.INFORMATION_MESSAGE);   	
		}
		else if(e.getSource() == btngmcc){			
                List<Factory> list=factoryService.getFactoyByTeamId(team.getId());           //获取当前小组可购买厂房
                List<String> type=new ArrayList<String>();
                for (int i=0;i<3;i++){
                	Factory factory=list.get(i);
                	if (factory.getStaus().equals("已购买")==false) type.add(factory.getType());
                }
              do{                                             
   				String s = (String) JOptionPane.showInputDialog(null,
						"请选择购买厂房类型:\n", "购买厂房", JOptionPane.PLAIN_MESSAGE, null,
						type.toArray(), type.get(0));
   				if (s==null) break;
   				else{
   					if (factoryService.buyFactoy(team.getId(),s)) 
   						 JOptionPane.showMessageDialog(null, "购买成功", "提示",JOptionPane.INFORMATION_MESSAGE); 
   					else
   						JOptionPane.showMessageDialog(null, "购买失败，当前现金不足", "提示",JOptionPane.INFORMATION_MESSAGE); 
   	                type=new ArrayList<String>();
   	                list=factoryService.getFactoyByTeamId(team.getId()); 
   	                for (int i=0;i<3;i++){
   	                	Factory factory=list.get(i);
   	                	if (factory.getStaus().equals("已购买")==false) type.add(factory.getType());
   	                }
   	                
   				}
               }while (type.isEmpty()==false);
				 this.dashBoardPanel.setDashBoardData();	   
		
			
		}
		// 变卖厂房
		else if (e.getSource() == btnbmcc) {
			List<Factory> list=factoryService.getTeamFactoyByStatus(team.getId(), "已购买");    //获取当前小组可购买厂房
            if (list.isEmpty()==false)
            {
            	List<String> type=new ArrayList<>();
            	for (Factory factory :list)
            		type.add(factory.getType());
   				String s = (String) JOptionPane.showInputDialog(null,
						"请选择变卖厂房类型:\n", "变卖厂房", JOptionPane.PLAIN_MESSAGE, null,
						type.toArray(), type.get(0));
   			    if(s!=null){
   			    	factoryService.sellFactoy(team.getId(),s);
   					JOptionPane.showMessageDialog(this, "变卖"+s+"成功", "提示",
							JOptionPane.INFORMATION_MESSAGE);   
   					this.dashBoardPanel.setDashBoardData();
   			   
            }
            }
            else  JOptionPane.showMessageDialog(this, "当前无可变卖厂房", "提示",JOptionPane.INFORMATION_MESSAGE);   
		}
		
		// 我的业务
		else if (e.getSource() == btnmyBiz) {
			new MyBusinessDialog();
		}
		else if (e.getSource() == btnmyReport) {
			
		}
	}
}
